// Filename:    Chat.cpp
// Author:      John Van Drasek
// Date:		3/11/2011
// Description:


// Includes
// Windows
#include <winsock2.h>
// NHO
#include "Chat.h"
#include "AutoJob_Client.h"

// AddPacketToList will accept packets from remote systems and create a linked list
// storing them for later use.  We will maintain a single packet stream for each user.
// If a user sends multiple packet streams without specifying anything to do with a
// previous stream.  The data will be deleted and the new stream will be stored.
void AddPacketToList(pkt in_pkt)
{
	// Get the user who sent the packet

	// Check to see if the user already has a linked list started

	// If the user does not already has a linked list started, the create a new linked list
	// and add the packet to the list

	// If the user already has a linked list started
		// If the incoming packet is marked as the first packet
			// delete the current list and create a new one with this packet as the first one
		// else
			// Add the packet to the end of the linked list

	// If the packet that just came in is the last packet in the stream
		// Execute the script associated with this packet stream
}

// receiveChatFromServer
//
//  Description: Receive text chat from the server and display in the
//                list box in the 3D client.
//
//  Input: void* mListBox - A CEGUI::Listbox pointer to the listbox
//          within the 3D client.
//  Output: void* - This function doesn't return anything but has a
//           return value from thread function call requirements.
//
void* ReceiveChatFromServer(void* mListBox)
{

	// Variable declarations
	int rc, sock, fromlen;
	char listBoxStr[BUFSIZE];
	struct sockaddr_in from;
	struct pkt inPkt;
	mListBox = NULL;

	// Variable assignments
	fromlen = sizeof(struct sockaddr_in);

	// Create INTERNET, udp datagram socket
	sock = rup_open();

	// Bind protocol to socket
	if(rup_bind(sock, CLIENTCHATPORT))
	{
		exit(1); 
	}

	// Loop forever reading and writing on port
	for(;;)
	{

		//  IMPORTANT!! inPkt is a struct which has memory allocated
		//    for it(static not dynamic).  A void pointer is returned
		//    from rup_read and is assigned to the memmory allocated
		//    to the struct.  Access the struct directly, not though
		//     indirection as with pointers to structs!
		// Read chat pkt from server
		if ((rc=rup_read(sock, &inPkt, sizeof(struct pkt), &from)) < 0 )
		{
			printf("chat server error: errno %d\n",errno);
			printf("reading datagram\n");
			exit(1);
		}

		// TODO: Read packets until we have received the entire stream.  We should be able
		//		 to receive packets from multiple remote systems at one time.
		AddPacketToList(inPkt);

		// Chat has been received from server!  Copy in to string and print for now
		memset(listBoxStr,0,BUFSIZE);
		strcat_s(listBoxStr, BUFSIZE, inPkt._msgbuf);
		printf("%s\n", listBoxStr);
		
		PrintMessage();
	}
}
